﻿using System.Collections.Generic;
using System.Linq;
using IGM.DataProvider;
using IGM.Framework.Constants;

namespace IGM.Business
{
    public partial class Business
    {
        #region -- Get Subcommittee Lecturer--

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public List<SubcommitteeLecturer> GetLecturerBySubcommitteeId(int subcommitteeId)
        {
            return DataContex != null && subcommitteeId > 0
                        ? DataContex.SubcommitteeLecturers.Where(m => m.IdSubcommittee == subcommitteeId).ToList() : null;
        }

        public List<SubcommitteeLecturer> GetSubcommitteeByLecturerId(int lecturerId)
        {
            return DataContex != null && lecturerId > 0
                        ? DataContex.SubcommitteeLecturers.Where(m => m.IdLecturer == lecturerId).ToList() : null;
        }

        public List<SubcommitteeLecturer> GetSubcommitteeLecturerByMajorId(int majorId)
        {
            return DataContex != null && majorId > 0
                        ? DataContex.SubcommitteeLecturers.Where(m => m.Lecturer.IdMajor == majorId).ToList()
                        : null;
        }

        #endregion

        #region -- Add Group Student--

        /// <summary>
        /// 
        /// </summary>
        /// <param name="subcommitteeLecturer"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool AddSubcommitteeLecturerValidator(SubcommitteeLecturer subcommitteeLecturer, out string message)
        {
            if (DataContex.SubcommitteeLecturers.Any(f=>f.IdSubcommittee == subcommitteeLecturer.IdSubcommittee && f.IdLecturer == subcommitteeLecturer.IdLecturer))
            {
                message = string.Format(ConstantMessages.ItemIsExisted, "Giảng viên thuộc tiểu ban");
                return false;
            }

            message = string.Empty;
            return true;
        }

        /// <summary>
        /// This function add a new facutly into list.
        /// </summary>
        public void AddSubcommitteeLecturer(SubcommitteeLecturer subcommitteeLecturer)
        {
            DataContex.SubcommitteeLecturers.AddObject(subcommitteeLecturer);
            DataContex.SaveChanges();
        }

        #endregion

        #region -- Edit Group Student --


        public bool EditSubcommitteeLecturerValidator(SubcommitteeLecturer subcommitteeLecturer, out string message)
        {
            var id = subcommitteeLecturer.Id;

            if (DataContex.SubcommitteeLecturers.Any(f => f.IdSubcommittee == subcommitteeLecturer.IdSubcommittee && f.IdLecturer == subcommitteeLecturer.IdLecturer && f.Id != id))
            {
                message = string.Format(ConstantMessages.ItemIsExisted, "Giảng viên thuộc tiểu ban");
                return false;
            }
            message = string.Empty;
            return true;
        }

        public bool EditSubcommitteeLecturer(SubcommitteeLecturer subcommitteeLecturer)
        {
            var id = subcommitteeLecturer.Id;
            var updateSubcommitteeLecturer = DataContex.SubcommitteeLecturers.SingleOrDefault(f => f.Id == id);

            if (updateSubcommitteeLecturer == null)
                return false;

            updateSubcommitteeLecturer.IdLecturer = subcommitteeLecturer.IdLecturer;

            DataContex.SaveChanges();
            return true;
        }

        #endregion

        #region -- Delete Group Lecturer --

        public bool DeleteSubcommitteeLecturerValidator(IEnumerable<SubcommitteeLecturer> subcommitteeLecturers, out string message)
        {
            const string intialize = "Giảng viên thuộc tiểu ban: ";

            var itemsCannotIdentify = ( subcommitteeLecturers.Select(lecturer => new { lecturer, id = lecturer.Id }).Where(
                @t => !DataContex.SubcommitteeLecturers.Any(m => m.Id == @t.id)).Select(@t => @t.lecturer)).Aggregate(intialize, (current, lecturer) => current + (lecturer.IdSubcommittee + ", "));

            if (itemsCannotIdentify != intialize)
            {
                message = string.Format(ConstantMessages.SomeItemsCanNotIdentify, itemsCannotIdentify.TrimEnd(','));
                return false;
            }

            message = string.Empty;
            return true;
        }

        /// <summary>
        /// Delete object
        /// </summary>
        public void DeleteSubcommitteeLecturer(List<SubcommitteeLecturer> subcommitteeLecturers)
        {
            for (var i = subcommitteeLecturers.Count - 1; i >= 0; i--)
            {
                var id = subcommitteeLecturers[i].Id;
                var deleteSubcommittee = DataContex.SubcommitteeLecturers.Single(m => m.Id == id);
                DataContex.SubcommitteeLecturers.DeleteObject(deleteSubcommittee);
            }
            DataContex.SaveChanges();
        }

        #endregion

    }
}